/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


/**
 *
 * @author dirk
 */
import java.util.*;
import java.io.*;
import java.lang.*;

public class GamePlay
{
	
	private static final String filename = "gameworld.dat";
	private GameWorld game;
	
	BufferedReader kb = new BufferedReader(new InputStreamReader(System.in));
	
	//non mobile	
	public static void main(String args[]) throws Exception
	{
		new GamePlay().play();
	}
	
	
	// Game demo constructor
	public void GamePlay() throws Exception
	{
	}


	public void play() throws Exception
	{              
        game = (new CreateGameWorld()).getGameWorld();
        Location locationOld = game.getCurrentLocation();
        Location locationNew = game.getCurrentLocation();
        ItemBag playerBag = new ItemBag();
                
		String command = null;
		boolean validCommand = false;

		//String input[] = {"n","s","w","e","s","w","n"};

		System.out.println((int)(math.Random()*5));
		System.out.println((int)(math.Random()*5));
		System.out.println((int)(math.Random()*5));
		System.out.println((int)(math.Random()*5));
		System.out.println((int)(math.Random()*5));
		System.out.println((int)(math.Random()*5));
		System.out.println((int)(math.Random()*5));
		System.out.println((int)(math.Random()*5));

		for ( ;;)//int i = 0; i < 7; i++)
		{
                    //Get the items from the location
                    String needItem = game.getCurrentLocation().getNeedItem();
                    String giveItem = game.getCurrentLocation().getItem();
					
                    locationOld = locationNew;
					locationNew = game.getCurrentLocation();
                    
					if(!(playerBag.containsItem(giveItem) || giveItem == null))
                    {
                        playerBag.addItem(giveItem);
                    }

                    System.out.println ("Current Items:\n" + playerBag.getItems());
                    
                    //Test if a blockage is in the way
                    if ( playerBag.containsItem(needItem) || needItem == null )
                    {    
                            game.showLocation();

                            // Get user input
                            try
                            {
                                    //command = input[i];
									System.out.println("Your move: ");
									command = kb.readLine();

                                    // Print a new line
                                    System.out.println();
                            }
                            catch (Exception e)
                            {
                            }

                            // By default, we haven't found a valid command
                            validCommand = false;

                            // Parse user input
                            if (command.length() == 0)
                            {
                                    System.out.println ("Huh? Invalid command!");
                                    continue;
                            }

                            // Convert to uppercase for comparison
                            command = command.toUpperCase();

                            // Search for an exit match
                            for (Enumeration e = game.getCurrentLocation().getExits().elements(); e.hasMoreElements();)
                            {
                                    Exit an_exit = (Exit) e.nextElement();

                                    if ( (an_exit.getDirectionName().compareTo(command) == 0) ||
                                             (an_exit.getShortDirectionName().compareTo(command) == 0 )
                                       )
                                    {
                                            // Set location to the location pointed to by exit
                                            game.setCurrentLocation( an_exit.getLeadsTo() );

                                            // Valid command encountered
                                            validCommand = true;

                                            // No need to search exits anymore
                                            break;
                                    }
                            }

                            // Check to see if user wants to quit
                            if (command.compareTo( "QUIT" ) == 0)
                            {
                                    System.out.println ("Okay. Bye!");
                                    System.exit(0);
                            }

                            // If no valid commands, warn the user is invalid
                            if (!validCommand)
                            {
                                    System.out.println ("Huh? Invalid direction!");
                                    System.out.println ();
                            }
                    }
                    else //If there is a blockage
                    {
                            game.showBlockage();
							System.out.println("Go back: ");
							kb.readLine();
							
                            game.setCurrentLocation(locationOld);
                    }


		}
	}
}
